<?php
/**
 * Created by PhpStorm.
 * User: fei
 * Date: 2016/8/23
 * Time: 11:02
 */

namespace Admin\Controller;
//use Think\Controller ;
use Component\AdminController;


class JibenxinxiController extends AdminController
{
    //增加企业基本信息
     public function append()
    {
       

        if (IS_POST) {
            $model = M('gq006');
            $data = $model->create();
            $res = $model->add();
            if ($res) {
                $info = $model->select();
                $this->assign("info",$info);
                $this->display("append");
                echo "<script type='text/javascript'>{alert('数据添加成功');}</script>";
               // echo "<script type='text/javascript'>alert('数据添加成功');history.back(-1);</script>";
            } else {
                echo "<script type='text/javascript'>alert('数据添加失败');history.back(-1);</script>";
            }
            return;
        }
        $this->display();
    }

    //根据用户的身份判断其所在公司，只显示其自己公司信息
    public function showlist0($mg_com){
        $model = M('gq006');
      //  $sql = "select * from sw_manager where mg_id=".$_SESSION['mg_id'];

//        $sql = "select * from gq006 where gq006.QA04==".$_COOKIE['mg_com'];
        $sql = "select * from gq006 where gq006.QA04='$mg_com'";
//print_r($sql);
        $info = $model->query($sql);
        //print_r($info);

        $this -> assign('info', $info);
        $this -> display();
}

//点击进入查询本企业基本信息模块
 public function showlist1()
    {
//        $model = M('gq006');
//        $data = $model->select();
//
        $this->display();
    }

//    public function showlist()
//    {
//        $model = M('gq006');
//        $data = $model->select();
//
//        $this->display();
//    }

//查询角色，查询所有企业基本信息（采用分页形式）
    function showlist(){

        $model = D("gq006");

        //1. 获得当前记录总条数
        $total = $model -> count();
        $per = 7;
        //2. 实例化分页类对象
        $page = new \Component\Page($total, $per); //autoload
        //3. 拼装sql语句获得每页信息
        $sql = "select * from gq006 ".$page->limit;
        $info = $model -> query($sql);
        //4. 获得页码列表
        $pagelist = $page -> fpage();

        $this -> assign('info', $info);
        $this -> assign('pagelist', $pagelist);
        $this -> display();
    }
    
    //企业填报人查询某一个企业的全部信息
    function showlistall($GQ006_ID){
        //$goods = D("Cominfo");
        $goods = D();

        //拼装sql语句获得每页信息
        $sql = "select * from gq001,gq002,gq003,gq004,gq005 ,gq006 where gq001.GQ006_ID=$GQ006_ID and gq002.GQ006_ID=$GQ006_ID 
                and gq003.GQ006_ID=$GQ006_ID and gq004.GQ006_ID=$GQ006_ID and gq005.GQ006_ID=$GQ006_ID and gq006.GQ006_ID=$GQ006_ID";
//print_r($sql);
        $info = $goods -> query($sql);
        //print_r($info);
        $this -> assign('info', $info);
        $this -> display();

    }
     //企业查询人查询某一个企业的全部信息
    function showlistall_c($GQ006_ID){
        //$goods = D("Cominfo");
        $goods = D();

        //拼装sql语句获得每页信息
        $sql = "select * from gq001,gq002,gq003,gq004,gq005 ,gq006 where gq001.GQ006_ID=$GQ006_ID and gq002.GQ006_ID=$GQ006_ID 
                and gq003.GQ006_ID=$GQ006_ID and gq004.GQ006_ID=$GQ006_ID and gq005.GQ006_ID=$GQ006_ID and gq006.GQ006_ID=$GQ006_ID";
//print_r($sql);
        $info = $goods -> query($sql);
        //print_r($info);
        $this -> assign('info', $info);
        $this -> display();

    }

    //企业封面信息列表展示（按年份2015）
    function coverinfoshowlist1(){

        $goods = D();
        //拼装sql语句获得每页信息
        $sql = "select * from gq006 where QA12 like '%2015%'";
        $info = $goods -> query($sql);
        $this -> assign('info', $info);
        $this -> display();
    }
    //企业封面信息列表展示（按年份2016）
    function coverinfoshowlist2(){

        $goods = D();
        //拼装sql语句获得每页信息
        $sql = "select * from gq006 where QA12 like '%2016%'";
        $info = $goods -> query($sql);
        $this -> assign('info', $info);
        $this -> display();
    }
    //企业填报人输入年份查询
    public function showbydate()
    {
        if (IS_POST) {

         $user = M('gq006');
          
            $QA12 = I('post.date');
         $sql = "select * from gq006 where     DATE (gq006.QA12)='$QA12'";
             $info = $user->query($sql);
            //print_r($info);

            $this->assign('info', $info);
            $this->display();

        }
    }
//企业查询人输入年份查询
    public function showbydate_c()
    {
        if (IS_POST) {

         $user = M('gq006');
          //  $user =D();
            $QA12 = I('post.date');
//print_r($QA12);

            $sql = "select * from gq006 where     DATE (gq006.QA12)='$QA12'";
            //$sql = "select * from gq006 where     'gq006.QA12'='$QA12'";
//print_r($sql);
            // $rst = $user->where(array('QA12' =>$QA12  ))->select();
            $info = $user->query($sql);
           //print_r($info);

            $this->assign('info', $info);
            $this->display();
//            $this -> display();
//
//            return;
//        }
            // $this -> assign('info', $info);

        }
    }

    //企业填报人修改自己的企业基本信息

    function upd($GQ006_ID){
        //查询被修改商品的信息并传递给模板展示
        $model = D("gq006");
        //两个逻辑：展示表单、收集表单
        if(IS_POST){
            $model -> create();
            $rst = $model -> save();
            if($rst){
                echo "<script type='text/javascript'>alert('数据修改成功');history.back(-1);</script>";;
            } else {
                echo "<script type='text/javascript'>alert('数据修改失败');history.back(-1);</script>";
            }
        } else {
            $info = $model->find($GQ006_ID); //一维数组
            $this -> assign('info', $info);
            $this -> display();
        }
    }

   //企业填报人删除自己企业的基本信息
    function del($GQ006_ID,$mg_com)
    {
        //查询被修改商品的信息并传递给模板展示
        $model = D("gq006");




        $res = $model->delete($GQ006_ID);

        if ($res) {

           // $this->redirect('Jibenxinxi/showlist', array(), 1, '数据删除成功');
//            $info = $model->select();
//            $this->assign("info", $info);
//            $this->display("showlist0");
            

            $sql = "select * from gq006 where gq006.QA04='$mg_com'";
            $info = $model->query($sql);
            $this -> assign('info', $info);
            $this -> display(showlist0);


//            $this->redirect('Jibenxinxi/showlist0', array(), 0.6, '数据删除成功');
            echo "<script type='text/javascript'>{alert('数据删除成功');}</script>";

        } else {
            echo "<script type='text/javascript'>alert('数据删除失败');history.back(-1);</script>";
        }

    }
//        //两个逻辑：展示表单、收集表单
//        if(IS_POST){
//            $model -> create();
//            $rst = $model -> save();
//            if($rst){
//                echo "<script type='text/javascript'>alert('数据修改成功');history.back(-1);</script>";;
//            } else {
//                echo "failure";
//            }
//        } else {
//            $info = $model->find($GQ006_ID); //一维数组
//            $this -> assign('info', $info);
//            $this -> display();
//        }



//导出企业基本信息表格
    public function output()
    {
//获取所需数据
        $res = M('gq006')->select();

        if (IS_POST) {


            $xlsName = "企业基本信息列表";//表名
            $xlsCell = array(
                array('QA18', '是否填写国家统计局一套表（QA18）'),
                array('QA03', '企业组织机构代码(QA03)') ,
                array('QA01', '报表编号（QA01）') ,
                array('QA19', '行政区划代码（QA19）'),

                array('QA04', '企业（单位）详细名称(QA04)'),
                array('QA15', '法人性质（QA15）'),
                array('QA05', '企业（单位）通讯地址(QA05)'),
                array('QA06', '邮政编码（QA06）'),
                array('QA07', '企业（单位）注册地址(QA07)'),

                array('QA08', '企业负责人(QA08)'),
                array('QA09', '联系电话（QA09）'),
                array('QA10', '传真(QA10)'),
                array('QA20', '统计负责人(QA20)'),
                array('QA11', '填报人（签章）(QA11)'),

                array('QA17', '填报人电话(QA17)'),
                array('QA12', '填报时间（QA12）'),
                array('QA13', 'E-mail(QA13)'),
                array('QA14', '网址(QA14)'),




            );
            $xlsData = array();
            foreach ($res as $key => $val) {
                array_push($xlsData, array(//这里的需要导出的内容，要注意键名跟上面的字段键名要一致
                    // 'QA04' => $val[GQ006_ID],
                    'QA18' => $val[QA18],
                    'QA03' => $val[QA03],
                    'QA01' => $val[QA01],
                    'QA19' => $val[QA19],

                    'QA04' => $val[QA04],
                    'QA15' => $val[QA15],
                    'QA05' => $val[QA05],
                    'QA06' => $val[QA06],
                    'QA07' => $val[QA07],

                    'QA08' => $val[QA08],
                    'QA09' => $val[QA09],
                    'QA10' => $val[QA10],
                    'QA20' => $val[QA20],
                    'QA11' => $val[QA11],

                    'QA17' => $val[QA17],
                    'QA12' => $val[QA12],
                    'QA13' => $val[QA13],
                    'QA14' => $val[QA14],



                ));
            }

            exportExcel($xlsName, $xlsCell, $xlsData);//这里就是调用写在function里的函数了。
        }
        die();
    }










}
